package com.wtx.chapter02;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

/**
 * @description: netty的socket编程
 * @author: wtx
 * @createDate: 2020/5/29
 */
public class MyServer {

    public static void main(String[] args) throws InterruptedException {

        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();

        try {
            ServerBootstrap bootstrap = new ServerBootstrap();
            bootstrap.group(bossGroup,workerGroup).channel(NioServerSocketChannel.class)
                    /*.handler(null)  //作用于 bossGroup*/
                    .childHandler(new MyServerInitializer());   //作用于 workerGroup

            ChannelFuture channelFuture = bootstrap.bind(8899).sync();
            // channelFuture.channel().close().sync(); 为什么这样写会直接关闭？
            channelFuture.channel().closeFuture().sync();     //获取到channel,关闭channel
        }finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }
}
